package com.itheima.controller;

import com.alibaba.fastjson.JSON;

import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.BlogSort;
import com.itheima.service.BlogSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * blog相关操作
 */

@RestController
@CrossOrigin
@RequestMapping("/blogSort")
public class BlogSortController {
    @Autowired
    private BlogSortService blogSortService;

    /**
     * 添加博客分类
     * @param blogSortVO
     * @return
     */
    @PostMapping("/add")
    @Transactional
    public Result add(@RequestParam Map<String,Object> blogSortVO){
        Result result = null;
        try{
            blogSortService.add(blogSortVO);
            result = new Result("success", "插入成功");
        }catch (RuntimeException e) {
            e.printStackTrace();
            result = new Result("error",e.getMessage());
        }
        return result;
    }

    /**
     * 通过引用量排序博客分类
     * @return
     */
    @PostMapping("/blogSortByCite")
    public Result blogSortByCite(){
        List<Map<String,Object>> blogSorts = blogSortService.blogSortByCite(); //可取出引用量
        return new Result("success",blogSorts);
    }

    /**
     * 通过点击量排序博客分类
     * @return
     */
    @PostMapping("/blogSortByClickCount")
    public Result blogSortByClickCount(){
      List<BlogSort> blogSorts = blogSortService.blogSortByClickCount();
      return new Result("success",blogSorts);

    }

    /**
     * 批量删除博客分类
     * @param blogSortVoList
     * @return
     */
    @PostMapping("/deleteBatch")
    @Transactional
    public Result deleteBatch(@RequestBody List<Map<String,Object>> blogSortVoList){
        try{
            int i = blogSortService.deleteBatch(blogSortVoList);
            if(i <= 0){
                return new Result<>("error","无此uid");
            }
            return new Result<>("success","操作成功");
        }catch (RuntimeException e){
            e.printStackTrace();
            Map<String, Object> map = new HashMap<>();
            map.put("success",0);
            map.put("message",e.getMessage());
            return new Result<>("00001",map);
        }
    }

    /**
     * 编辑博客分类
     * @param blogSort
     * @return
     */
    @PostMapping("/edit")
    @Transactional
    public String edit(@RequestBody Map<String,Object> blogSort){
        Map<String,Object> map = new HashMap<>();
        try{
            blogSortService.edit(blogSort);
            return JSON.toJSONString(new Result<>("success","操作成功"));
        }catch(RuntimeException e){
            e.printStackTrace();
            String message = e.getMessage();
            map.put("success",0);
            map.put("resultCode","00001");
            map.put("message",message);
            return JSON.toJSONString(map);
        }

    }

    /**
     * 获取博客分类列表
     * @param blogSortV0
     * @return
     */
    @PostMapping("/getList")
    public Result getList(@RequestBody Map<String,Object> blogSortV0){
        PageResult<BlogSort> blogSortsResult = blogSortService.getList(blogSortV0);
        return new Result<>("success",blogSortsResult);
    }

    /**
     * 置顶分类
     * @return
     */
    @PostMapping("/stick")
    public Result stick(@RequestBody  Map<String,Object> blogSortV0){
        try{
           blogSortService.stick(blogSortV0);
            return new Result<>("success","操作成功");
        } catch(RuntimeException e){
            String message = e.getMessage();
            return new Result<>("error",message);
        }
    }

}
